package com.finger.forest.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.finger.forest.R;
import com.finger.forest.adapter.FireInfoAdapter;
import com.finger.forest.adapter.SynchronizationDataAdapter;
import com.finger.forest.base.BaseFragment;
import com.finger.forest.base.BaseView;
import com.finger.forest.base.CommonKey;
import com.finger.forest.base.ResponseTag;
import com.finger.forest.http.presenter.RegisterPresenter;
import com.finger.forest.model.CollectionRoadModel;
import com.finger.forest.model.FireInfoModel;
import com.finger.forest.utils.StringUtil;
import com.finger.forest.utils.ToastUtils;
import com.finger.forest.utils.UniversalItemDecoration;
import com.finger.forest.utils.encode.PrefsUtil;
import com.jakewharton.rxbinding2.view.RxView;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 数据同步-火场
 */
public class FireFragment extends BaseFragment<RegisterPresenter> implements BaseView {

    private Unbinder unbinder;


    @BindView(R.id.rvData)
    RecyclerView rvData;

    @BindView(R.id.tvAllSynchronization)
    TextView tvAllSynchronization;

    private FireInfoAdapter fireInfoAdapter;

    private List<FireInfoModel> fireInfoModels = new ArrayList<>();

    @Override
    protected void receiveData() {

    }

    @Override
    protected void initializeNavigation() {

    }

    @Override
    protected void initializeView() {
        List<FireInfoModel> fireInfoModelList = LitePal.findAll(FireInfoModel.class);
        fireInfoModels.clear();
        fireInfoModels.addAll(fireInfoModelList);
        if (null != fireInfoModels && fireInfoModels.size() > 0) {
            fireInfoAdapter = new FireInfoAdapter(R.layout.synchronization_data_item_view, fireInfoModels);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
            rvData.setLayoutManager(linearLayoutManager);
            rvData.setAdapter(fireInfoAdapter);
            rvData.addItemDecoration(new UniversalItemDecoration() {
                @Override
                public Decoration getItemOffsets(int position) {
                    ColorDecoration colorDecoration = new ColorDecoration();
                    colorDecoration.bottom = getResources().getDimensionPixelOffset(R.dimen.dp_1);
                    colorDecoration.decorationColor = getResources().getColor(R.color.blackF5);
                    return colorDecoration;
                }
            });
            fireInfoAdapter.notifyDataSetChanged();
        }

        clicks();
    }

    @Override
    protected void loadDataFromServer() {

    }

    private int fireId = 0;

    private void clicks() {
        //全部同步
        RxView.clicks(tvAllSynchronization).throttleFirst(2, TimeUnit.SECONDS).subscribe(o -> {

            for (FireInfoModel fireInfoModel : fireInfoModels) {
                if (fireInfoModel.isSelect()) {
                    fireId = fireInfoModel.getId();
                    saveFireInfo(fireInfoModel);
                    break;
                }
            }
        });
    }

    private void saveFireInfo(FireInfoModel fireInfoModel) {
        Map<String, Object> maps = new HashMap<>();
        maps.put("userid", userId);
        maps.put("token", token);
        maps.put("firelocation", fireInfoModel.getFirelocation());
        maps.put("cloudsstatus", haveClouds(fireInfoModel.getCloudsstatus()));
        maps.put("district", "1");
        maps.put("iscontinuity", haveContinuousOccurrence(fireInfoModel.getIscontinuity()));
        maps.put("firesitetype", fireInfoModel.getFiresitetype());
        maps.put("firesity", fireInfoModel.getFiresity());
        maps.put("firenumber", fireInfoModel.getFirenumber());
        maps.put("remake", fireInfoModel.getRemake());

        showPd();
        presenter.fireInformationInsert(maps);
    }

    private String haveClouds(String clouds) {
        String c = "";
        if (clouds.equals("有")) {
            c = "0";
        } else {
            c = "1";
        }
        return c;
    }

    private String haveContinuousOccurrence(String clouds) {
        String c = "";
        if (clouds.equals("是")) {
            c = "0";
        } else {
            c = "1";
        }
        return c;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_fire, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (null != unbinder) {
            unbinder.unbind();
        }
    }

    @Override
    public void onSuccess(Object model, int tag, String msg) {
        switch (tag) {
            case ResponseTag.FIRE_INFOMATION_INSERT:
                FireInfoModel fireInfoModel = (FireInfoModel) model;
                if (null != fireInfoModel) {
                    //同步成功数据库删除
                    LitePal.delete(FireInfoModel.class, fireId);

                    //显示的数据也清除
                    if (fireInfoModels.size() > 0) {
                        for (int i = 0; i < fireInfoModels.size(); i++) {
                            if (fireInfoModels.get(i).getId() == fireId) {
                                fireInfoModels.remove(fireInfoModels.get(i));
                            }
                        }
                    }

                    //如果还有选中的数据，继续上传
                    if (fireInfoModels.size() > 0) {
                        for (FireInfoModel roadModel : fireInfoModels) {
                            if (roadModel.isSelect()) {
                                fireId = roadModel.getId();
                                saveFireInfo(roadModel);
                                break;
                            }
                        }
                    }

                    //刷新列表
                    fireInfoAdapter.notifyDataSetChanged();
                }

                ToastUtils.show(msg);
                break;

            default:
                break;
        }
        hidePd();
    }

    @Override
    public void onError(int errcode, int tag, String message) {
        ToastUtils.show(message);
        hidePd();
    }
}